package com.sicheng.datastruct.stack;

public class ArrayStack {
    private final int MAX_SIZE;//栈的最大容量
    private final int[] stack;//数组模拟栈
    private int top;    //栈顶，栈的移动端，初始值为-1

    public ArrayStack(int MAX_SIZE) {
        this.MAX_SIZE = MAX_SIZE;
        this.top = -1;
        stack = new int[MAX_SIZE];
    }

    public void push(int push) {
        if (isFull()) {
            return;
        }
        stack[++top] = push;
    }

    public int pop() {
        if (isEmpty()) {
            throw new RuntimeException("栈为空");
        }
        return stack[top--];
    }

    private boolean isEmpty() {
        return top == -1;
    }

    private boolean isFull() {
        return top == MAX_SIZE - 1;
    }

    /**
     * 遍历时从栈顶开始遍历
     */
    public void displayStack() {
        if (isEmpty()) {
            throw new RuntimeException("栈为空");
        }

        int current = top;
        while (current != -1) {
            System.out.printf("stack[%d]=%d%n", current, stack[current--]);
        }


    }


}
